#include "Avalon/SignalHandling/QueuedSignalHandler.hpp"
#include "Avalon/Base/Algorithm.hpp"

using namespace Avalon;
using namespace Avalon::SignalHandling;
using namespace boost;
using namespace std;

QueuedSignalHandler::BaseCallback::~BaseCallback() {}

QueuedSignalHandler::QueuedSignalHandler() {}

QueuedSignalHandler::~QueuedSignalHandler() {
  Clear(m_slots);
}

void QueuedSignalHandler::HandleSignals() {
  vector<BaseCallback*> slots;
  {
    lock_guard<mutex> lock(m_mutex);
    std::swap(slots, m_slots);
  }
  for(vector<BaseCallback*>::const_iterator i = slots.begin();
      i != slots.end(); ++i) {
    (*i)->Invoke();
    delete *i;
  }
}

void QueuedSignalHandler::SetQueuedSlot(const QueuedSlot& queuedSlot) {
  lock_guard<mutex> lock(m_mutex);
  m_queuedSlot = queuedSlot;
}
